package com.tianjs.classload_lab;

import java.io.IOException;
import java.io.InputStream;

public class ClassLoaderTest {

	public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		ClassLoader myLoader = new ClassLoader() {
			@Override
			public Class<?> loadClass(String name) throws ClassNotFoundException {
				String filename = name.substring(name.lastIndexOf(".")+1)+".class";
				InputStream is = getClass().getResourceAsStream(filename);
				if(is == null) {
					return super.loadClass(name);
				}
				try {
					byte[] b = new byte[is.available()];
					is.read(b);
					return defineClass(name, b, 0, b.length);
				} catch (IOException e) {
					throw new ClassNotFoundException(name);
				}
			}
		};
		Object obj=myLoader.loadClass("com.tianjs.classload_lab.SuperClass").newInstance();
		System.out.println(obj.getClass());
		//返回false,
		//obj对象的类是自定义类加载器加载的类，
		//com.tianjs.classload_lab.SuperClass为系统应用程序类加载器加载的类
		System.out.println(obj instanceof com.tianjs.classload_lab.SuperClass);
	}

}
